//Erstellt von Moritz Liepe
package main.editor;



import main.Module;
import main.simulation.SetModulePosition;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.handlers.HandlerUtil;


public class UserMoveHandler extends AbstractHandler 
{

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException 
	{
		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
		if (selection != null & selection instanceof IStructuredSelection) 
		{
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
			if(! strucSelection.isEmpty() && strucSelection.getFirstElement() instanceof Module)
			{
				final Module selectionObject = (Module)strucSelection.getFirstElement();
				final Point oldLoc = ((Module)selectionObject).getLocation();
				Thread moveThread = new Thread(new Runnable(){

					@Override
					public void run() {
						for(int i = 0; i <4; i++){
							switch(i){
							case 0:
								Display.getDefault().asyncExec(new SetModulePosition(((Module)selectionObject),new Point(oldLoc.x +100, oldLoc.y)));
								try
								{
									Thread.sleep(1000);
								}
								catch(Exception e)
								{
									e.printStackTrace();
								}
								break;
							case 1:
								Display.getDefault().asyncExec(new SetModulePosition(((Module)selectionObject),new Point(oldLoc.x +100, oldLoc.y +100)));
								try
								{
									Thread.sleep(1000);
								}
								catch(Exception e)
								{
									e.printStackTrace();
								}
								break;
							case 2:
								Display.getDefault().asyncExec(new SetModulePosition(((Module)selectionObject),new Point(oldLoc.x, oldLoc.y +100)));
								try
								{
									Thread.sleep(1000);
								}
								catch(Exception e)
								{
									e.printStackTrace();
								}
								break;
							case 3:
								Display.getDefault().asyncExec(new SetModulePosition(((Module)selectionObject),new Point(oldLoc.x, oldLoc.y)));
								try
								{
									Thread.sleep(1000);
								}
								catch(Exception e)
								{
									e.printStackTrace();
								}
								break;
							}
						}
					}
				});
				moveThread.start();
			}
		}
		
		return null;
	}

}
